﻿// <copyright file="EventToCommand.cs" company="Skype Limited">
//
// Copyright (C) 2011, Skype Limited
//
// All intellectual property rights, including but not limited to copyrights,
// trademarks and patents, as well as know how and trade secrets contained in,
// relating to, or arising from the internet telephony software of
// Skype Limited (including its affiliates, "Skype"), including without
// limitation this source code, Skype API and related material of such
// software proprietary to Skype and/or its licensors ("IP Rights") are and
// shall remain the exclusive property of Skype and/or its licensors.
// The recipient hereby acknowledges and agrees that any unauthorized use of
// the IP Rights is a violation of intellectual property laws.
//
// Skype reserves all rights and may take legal action against infringers of
// IP Rights.
//
// The recipient agrees not to remove, obscure, make illegible or alter any
// notices or indications of the IP Rights and/or Skype's rights and
// ownership thereof.
// </copyright>
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace WP8Demos.Helpers
{
    public class EventToCommand : TriggerAction<DependencyObject>
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new PropertyMetadata(null));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(EventToCommand), new PropertyMetadata(null));

        public static readonly DependencyProperty EventArgsProperty =
                    DependencyProperty.Register("EventArgs", typeof(object), typeof(EventToCommand), new PropertyMetadata(null));

        public object EventArgs
        {
            get { return (object)GetValue(EventArgsProperty); }
            set { SetValue(EventArgsProperty, value); }
        }

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        protected override void Invoke(object parameter)
        {
            this.EventArgs = parameter;

            if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
            {
                this.Command.Execute(this.CommandParameter);
            }
        }
    }
}
